package com.bianguo.android.apushdemo.view.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.entity.Data;
import com.bianguo.android.apushdemo.entity.TestEntity;
import com.bianguo.android.apushdemo.presenter.impl.BasePresenter;
import com.bianguo.android.apushdemo.presenter.impl.LocationPresenter;
import com.bianguo.android.apushdemo.presenter.impl.TestPresenter;
import com.bianguo.android.apushdemo.service.DemoIntentService;
import com.bianguo.android.apushdemo.service.DemoPushService;
import com.bianguo.android.apushdemo.utils.GlideUtil;
import com.bianguo.android.apushdemo.utils.MyLog;
import com.bianguo.android.apushdemo.utils.PermissionHelper;
import com.bianguo.android.apushdemo.view.wiget.popupwindow.BasePopupWindow;
import com.bianguo.android.apushdemo.view.wiget.popupwindow.CommentPopup;
import com.google.gson.Gson;
import com.igexin.sdk.PushManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, BasePopupWindow.OnClickBtnListener {

    @BindView(R.id.title_left_ib)
    ImageButton titleLeftIb;
    @BindView(R.id.main_push_text)
    TextView pushTextTv;
    @BindView(R.id.title_right_btn)
    CheckBox titleRightCb;
    @BindView(R.id.main_tab_layout)
    XTabLayout mTabLayout;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.main_anim_test)
    TextView animTv;
    @BindView(R.id.main_image)
    ImageView imageView;
    @BindView(R.id.main_anim_btn)
    Button animBtn;

    private final String IMAGE_URL = "http://sc.bianguo.com.cn/scnews/Uploads/avatar/2017-06-21/594a333991c84.jpg";
    private CommentPopup commentPopup;
    private TestPresenter testPresenter;
    private LocationPresenter locationPresenter;

    @Override
    public int layoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        titleLeftIb.setVisibility(View.GONE);
        titleRightCb.setVisibility(View.VISIBLE);
        boolean permission = PermissionHelper.getInstance(this)
                .isPermission(this, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.RECEIVE_BOOT_COMPLETED
                }, PermissionHelper.PERMISSION_READ_EXTERNAL_STORAGE);
        MyLog.mLog("permission = " + permission);
        PushManager.getInstance().initialize(this.getApplicationContext(), DemoPushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
        String clientId = PushManager.getInstance().getClientid(this);
        MyLog.mLog("clientId = " + clientId);
        parseManifests();
        switch (spu.getTheme()) {
            case 1:
                titleRightCb.setChecked(false);
                break;
            case 0:
                titleRightCb.setChecked(true);
                break;
        }
        titleRightCb.setOnCheckedChangeListener(this);
        mTabLayout.setxTabDisplayNum(5);
        for (int i = 0; i < 20; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText("" + i));
        }
        GlideUtil.loadImage(mContext, imageView, IMAGE_URL, 0);


    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @OnClick({R.id.main_btn, R.id.text1, R.id.text2, R.id.text3, R.id.text4, R.id.main_image, R.id.main_anim_btn,
            R.id.main_push_text})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_btn:
                startActivity(new Intent(mContext, CalendarActivity.class));
                break;
            case R.id.main_image:
                ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, view,
                        getResources().getString(R.string.me_image));
                startActivity(new Intent(mContext, MeActivity.class), optionsCompat.toBundle());
                break;
            case R.id.text1:
                doShow();
                if (locationPresenter == null) {
                    locationPresenter = new LocationPresenter();
                }
                locationPresenter.location("杭州市下城区东新路545号", "json", "37492c0ee6f924cb5e934fa08c6b1676");
//                locationPresenter.location("http://api.map.baidu.com/geocoder?address=4%E5%8F%B7%E5%A4%A7%E8%A1%975%E5%8F%B7%E8%B7%AF%E5%8F%A3&output=json&key=37492c0ee6f924cb5e934fa08c6b1676&city=%E6%9D%AD%E5%B7%9E%E5%B8%82");
                if (text1.isSelected()) {
                    return;
                }
                text1.setSelected(true);
                text2.setSelected(false);
                text3.setSelected(false);
                text4.setSelected(false);
                break;
            case R.id.text2:
                doHide();
                if (text2.isSelected()) {
                    return;
                }
                text1.setSelected(false);
                text2.setSelected(true);
                text3.setSelected(false);
                text4.setSelected(false);
                startActivity(new Intent(mContext, ScheduleActivity.class));
                break;
            case R.id.text3:
                if (text3.isSelected()) {
                    return;
                }
                text1.setSelected(false);
                text2.setSelected(false);
                text3.setSelected(true);
                text4.setSelected(false);
                startActivity(new Intent(mContext, MovieActivity.class));
                break;
            case R.id.text4:
                showCommentPopup();
                if (text4.isSelected()) {
                    return;
                }
                text1.setSelected(false);
                text2.setSelected(false);
                text3.setSelected(false);
                text4.setSelected(true);
                break;
            case R.id.main_anim_btn:
                startActivity(new Intent(mContext, SuspensionActivity.class));
                break;
            //底部弹出收缩
            case R.id.main_push_text:
                startActivity(new Intent(mContext, MapActivity.class));
                break;
        }
    }

    private void showCommentPopup() {
        if (commentPopup == null) {
            commentPopup = new CommentPopup(mContext);
            commentPopup.setOnClickBtnListener(this);
        }
        commentPopup.showPopupWindow(text4);
    }

    private void doHide() {
        TranslateAnimation animation = new TranslateAnimation(0, animTv.getWidth(), 0, 0);
        animation.setDuration(500);
        animation.setFillAfter(false);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animTv.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animTv.startAnimation(animation);
    }

    private void doShow() {
        animTv.setVisibility(View.VISIBLE);
        TranslateAnimation animation = new TranslateAnimation(0, 0, -animTv.getHeight(), 0);
        animation.setDuration(500);
        animation.setFillAfter(true);
        animTv.startAnimation(animation);
    }

    private void parseManifests() {
        String packageName = getApplicationContext().getPackageName();
        try {
            ApplicationInfo appInfo = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            if (appInfo.metaData != null) {
                String appid = appInfo.metaData.getString("PUSH_APPID");
                String appsecret = appInfo.metaData.getString("PUSH_APPSECRET");
                String appkey = appInfo.metaData.getString("PUSH_APPKEY");
                MyLog.mLog("appid = " + appid + ", appsecret = " + appsecret + ", appkey = " + appkey);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        MyLog.mLog("isChecked = " + isChecked);
        if (isChecked) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            spu.setTheme(0);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            spu.setTheme(1);
        }
        recreate();
    }

    @Override
    public void onClickBtn(View view) {
        switch (view.getId()) {
            case R.id.popup_select_1:
                startActivity(new Intent(mContext, DesignActivity.class));
                break;
            case R.id.popup_select_2:
                startActivity(new Intent(mContext, ChartActivity.class));
                break;
            case R.id.popup_select_4:
                if (testPresenter == null) {
                    testPresenter = new TestPresenter();
                }
                Data data = new Data();
                data.setId("1");
                TestEntity testEntity = new TestEntity();
                testEntity.setRequestMethod("getAuditLoggingById");
                testEntity.setData(data);
//                String jsonStr = new Gson().toJson(testEntity);
//                String jsonStr = "{\n" +
//                        "\"RequestMethod\":\"getSxbzxr\",\n" +
//                        "\"data\":{\"fbrq_ks\":\"2016-01-31\",\"fbrq_js\":\"2016-2-15\"},\n" +
//                        "\"cols\":[\"sanh\",\"sabh\",\"fydm\",\"fymc\",\"ah\",\"sxbzxrmc\",\"sxbzxrxh\",\n" +
//                        "        \"sxbzxrzjhm\",\"sxid\",\"fbrq\",\"flwsqdyw\",\"bzxrlxqk\",\n" +
//                        "        \"type\",\"ip\",\"czyh\",\"bz\",\"czrq\",\"lx\",\"cjjlsj\",\n" +
//                        "\t\t\"cbr_name\",\"cbr_mail\",\"cbr_userid\",\"cbr_sjhm\"],\n" +
//                        "\"pageSize\":\"2\",\n" +
//                        "\"curPage\":\"1\"\n" +
//                        "}";
                String jsonStr = "{\n" +
                        "\"RequestMethod\":\"insertComment\",\n" +
                        "\"data\":{\"fbsqr_name\":\"李科\",\"fbsqr_title\":\"悬赏XXX\",\"fbsqr_contentid\":\"02\",\"fbsqr_comment\":\"\",\n" +
                        "        \"jbr_id\":\"1\",\"jbr_idcard\":\"6222021001116245702\",\"jbr_name\":\"李四\",\"jbr_comment\":\"XXX于2017年XX月XX分在武林广场附近....\",\n" +
                        "\t\t\"is_public\":\"0\",\"comment_role\":\"2\"},\n" +
                        "\"cols\":[\"sanh\",\"sabh\",\"fydm\",\"fymc\",\"ah\",\"sxbzxrmc\",\"sxbzxrxh\",\n" +
                        "        \"sxbzxrzjhm\",\"sxid\",\"fbrq\",\"flwsqdyw\",\"bzxrlxqk\",\n" +
                        "        \"type\",\"ip\",\"czyh\",\"bz\",\"czrq\",\"lx\",\"zx_cjjlsj\",\n" +
                        "\t\t\"cbr_name\",\"cbr_mail\",\"cbr_userid\",\"cbr_sjhm\"],\n" +
                        "}";
                MyLog.mLog("jsonStr = " + jsonStr);
//                RequestBody body = RequestBody.create(MediaType.parse(""), jsonStr);
//                testPresenter.test(body);
                break;
        }
    }
}
